package com.leyou.item.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.dto.PageResult;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.mapper.BrandMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author 虎哥
 */
@Service
public class BrandService {

    @Autowired
    private BrandMapper brandMapper;

    public PageResult<BrandDTO> queryBrandByPage(String key, Integer page, Integer rows, String sortBy, Boolean desc) {
        // 1.分页 LIMIT 0, 10
        PageHelper.startPage(page, rows);

        // 2.准备搜索条件
        Example example = new Example(Brand.class);
        // 2.1.过滤条件 WHERE `name` LIKE "%x%" OR letter = 'X'
        if(StringUtils.isNotBlank(key)) {
            example.createCriteria().orLike("name", "%" + key + "%")
                    .orEqualTo("letter", key);
        }
        // 2.2.排序条件 ORDER BY update_time
        String orderByClause = sortBy + (desc ? " DESC" : " ASC");
        example.setOrderByClause(orderByClause);

        // 3.查询数据，得到结果
        List<Brand> list = brandMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.RESOURCE_NOT_FOUND);
        }

        // 4.封装数据
        // 4.1.分页结果
        PageInfo<Brand> info = new PageInfo<>(list);
        // 4.2.dto转换
        List<BrandDTO> dtoList = BeanHelper.copyWithCollection(list, BrandDTO.class);

        return new PageResult<>(info.getTotal(), info.getPages(), dtoList);
    }

    @Transactional
    public void saveBrand(BrandDTO brandDTO) {
        // 对品牌转换
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        // 新增品牌
        brandMapper.insertSelective(brand);
        // 新增中间表
        List<Long> cids = brandDTO.getCids();

        brandMapper.insertCategoryBrand(cids, brand.getId());
    }

    public BrandDTO queryById(Long id){
        Brand brand = brandMapper.selectByPrimaryKey(id);
        if (brand == null) {
            throw new LyException(ExceptionEnum.RESOURCE_NOT_FOUND);
        }
        return BeanHelper.copyProperties(brand, BrandDTO.class);
    }

    public List<BrandDTO> queryByCategory(Long id) {
        List<Brand> list = brandMapper.queryByCategoryId(id);
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.RESOURCE_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, BrandDTO.class);
    }

    public List<BrandDTO> queryBrandByIds(List<Long> idList) {
        List<Brand> list = brandMapper.selectByIdList(idList);
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.RESOURCE_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, BrandDTO.class);
    }
}
